using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;
using Microformats.Entities;
using Microformats.Interfaces;
using Microformats.Readers;
using Field=Microformats.Entities.Field;

namespace hCard_Display
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string url = txtUrl.Text;

            if(url.StartsWith("http://") == false)
            {
                url = "http://" + url;
            }

            if (url != null)
            {
                Uri u;
                
                try
                {u = new Uri(url);}
                catch(UriFormatException uriEx)
                {
                    MessageBox.Show("The Uri is invalid.\nReason:" + uriEx.Message);
                    return;
                }


                HttpWebRequest wr = (HttpWebRequest) WebRequest.Create(u);
                using(HttpWebResponse wresp = (HttpWebResponse) wr.GetResponse())
                {
                    using(MicroformatReader mr = new MicroformatReader(wresp.GetResponseStream(), InputSourceType.Html, u.GetComponents(UriComponents.Scheme|UriComponents.Host, UriFormat.SafeUnescaped)))
                    {
                        Microformats.Entities.Microformat m;
                        while((m = mr.Read()) != null)
                        {
                            ListViewItem lvi = new ListViewItem(m.Name);
                            lvi.Tag = m; //Attach The microformat to the ListViewItem
                            lstMicroformats.Items.Add(lvi);
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Please Enter a URL.");
            }
        }

        private void lstMicroformats_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            //Get the SelectedListViewItem
            Microformat m = (Microformat) e.Item.Tag;

            //Remove all the previous entries
            treeView1.Nodes.Clear();

            TreeNode root = new TreeNode(m.Name);

            treeView1.BeginUpdate();

            treeView1.Nodes.Add(root);
            AddChildFields(m.Fields, root);
            treeView1.EndUpdate();
        }

        private void AddChildFields(List<IField> fields, TreeNode currentNode)
        {
            foreach (IField f in fields)
            {
                TreeNode newNode = new TreeNode(f.Name + ":" + f.MachineValue);
                currentNode.Nodes.Add(newNode);

                if (f.Fields.Count > 0)
                {
                    AddChildFields(f.Fields, newNode);
                }
            }
        }
    }
}